﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using BeanServices;
using Windows.UI;
using Windows.UI.Core;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace BeanScratchDemo
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        BeanScratchService _scratchService = new BeanScratchService();

        public MainPage()
        {
            this.InitializeComponent();

            _scratchService.DeviceConnectionUpdated += _scratchService_DeviceConnectionUpdated;
            _scratchService.Scratch2ChangeCompleted += _scratchService_Scratch2ChangeCompleted;
        }

        async void _scratchService_DeviceConnectionUpdated(bool isConnected)
        {
            if (isConnected)
            {
                await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    ConnectionStateTextBlock.Text = "(connected)";
                    ConnectionStateTextBlock.Foreground = new SolidColorBrush(Colors.Green);
                });
            }
            else
            {
                await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    ConnectionStateTextBlock.Text = "(disconnected)";
                    ConnectionStateTextBlock.Foreground = new SolidColorBrush(Colors.Red);
                });
            }
        }

        async void _scratchService_Scratch2ChangeCompleted(BeanServices.ScratchData scratchData)
        {
            var temp = scratchData.Data[0];
            await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => 
            {
                ScratchValueTextBlock.Text = "Temperature: " + temp.ToString() + " °C";
            });
        }

        protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
 	        base.OnNavigatedTo(e);

            var devices = await _scratchService.GetListOfDevices();
            var device = devices[0];
            await _scratchService.InitializeService(device);
            DeviceTextBlock.Text = device.Name;

            var scratchData = await _scratchService.ReadScratch2Async();
            var temp = scratchData.Data[0];
            await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                ScratchValueTextBlock.Text = "Temperature: " + temp.ToString() + " °C";
            });
        }

        private async void LedToggleSwitch_Toggled(object sender, RoutedEventArgs e)
        {
            byte data = LedToggleSwitch.IsOn ? (byte)1 : (byte)0;
            ScratchData scracthData = new ScratchData(new byte[] {data});
            await _scratchService.WriteScratch1Async(scracthData);
        }

    }
}
